package com.gztms.blogmgnt.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gztms.blogmgnt.mapper.BlogTypeMapper;
import com.gztms.blogmgnt.pojo.Blog;
import com.gztms.blogmgnt.pojo.BlogStatus;
import com.gztms.blogmgnt.pojo.BlogType;
import com.gztms.blogmgnt.pojo.vo.BlogTypeVO;
import com.gztms.blogmgnt.service.BlogService;
import com.gztms.blogmgnt.service.BlogTypeService;
import com.gztms.utils.BeanCopyUtils;
import com.gztms.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author Zoey
 * @date 2023/5/1 17:40
 * @Description TODO
 **/
@Service
public class BlogTypeServiceImpl extends ServiceImpl<BlogTypeMapper, BlogType> implements BlogTypeService {
    @Autowired
    private BlogService blogService;

    @Override
    public ResultUtil getTypeList() {
        // 1、查询文章表，状态已审核
        LambdaQueryWrapper<Blog> blogWrapper = new LambdaQueryWrapper();
        blogWrapper.eq(Blog::getStatus, BlogStatus.BLOG_STATUS_SUCCESS);
        List<Blog> blogList = blogService.list(blogWrapper);

        // 2、根据文章的分类id，并且去重
        Set<Integer> typeIds = blogList.stream()
                .map(blog -> blog.getBid())
                .collect(Collectors.toSet());

        // 3、查询分类表
        List<BlogType> blogTypes = listByIds(typeIds);
        List<BlogTypeVO> blogTypeVOS = BeanCopyUtils.copyBeanList(blogTypes, BlogTypeVO.class);
        Map<String, Object> map = new HashMap<>();
        map.put("blogTypes",blogTypeVOS);
        return new ResultUtil(ResultUtil.SUCCESS, "查询成功", map);
    }
}
